<!DOCTYPE html>
<html>

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>原生JS实现购物车结算功能代码</title>
    <link rel="stylesheet" href="../css/style.css" />
</head>

<body>
    <div class="catbox">
        <table id="cartTable">
            <thead>
                <tr>
                    <th><label>
                            <input class="check-all check" type="checkbox" />&nbsp;&nbsp;全选</label></th>
                    <th>商品</th>
                    <th>单价</th>
                    <th>数量</th>
                    <th>小计</th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td class="checkbox"><input class="check-one check" type="checkbox" /></td>
                    <td class="goods"><img src="../images/1.jpg" alt="" /><span>Casio/卡西欧 EX-TR350</span></td>
                    <td class="price">5999.88</td>
                    <td class="count"><span class="reduce"></span>
                        <input class="count-input" type="text" value="1" />
                        <span class="add">+</span>
                    </td>
                    <td class="subtotal">5999.88</td>
                    <td class="operation"><span class="delete">删除</span></td>
                </tr>
                <tr>
                    <td class="checkbox"><input class="check-one check" type="checkbox" /></td>
                    <td class="goods"><img src="../images/2.jpg" alt="" /><span>Canon/佳能 PowerShot SX50 HS</span></td>
                    <td class="price">3888.50</td>
                    <td class="count"><span class="reduce"></span>
                        <input class="count-input" type="text" value="1" />
                        <span class="add">+</span>
                    </td>
                    <td class="subtotal">3888.50</td>
                    <td class="operation"><span class="delete">删除</span></td>
                </tr>
                <tr>
                    <td class="checkbox"><input class="check-one check" type="checkbox" /></td>
                    <td class="goods"><img src="../images/3.jpg" alt="" /><span>Sony/索尼 DSC-WX300</span></td>
                    <td class="price">1428.50</td>
                    <td class="count"><span class="reduce"></span>
                        <input class="count-input" type="text" value="1" />
                        <span class="add">+</span>
                    </td>
                    <td class="subtotal">1428.50</td>
                    <td class="operation"><span class="delete">删除</span></td>
                </tr>
                <tr>
                    <td class="checkbox"><input class="check-one check" type="checkbox" /></td>
                    <td class="goods"><img src="../images/4.jpg" alt="" /><span>Fujifilm/富士 instax mini 25</span></td>
                    <td class="price">640.60</td>
                    <td class="count"><span class="reduce"></span>
                        <input class="count-input" type="text" value="1" />
                        <span class="add">+</span>
                    </td>
                    <td class="subtotal">640.60</td>
                    <td class="operation"><span class="delete">删除</span></td>
                </tr>
            </tbody>
        </table>
        <div class="foot" id="foot">

            <a class="fl" id="deleteAll" href="javascript:;">删除</a>
            <div class="fr closing" onclick="getTotal();">结 算</div>
            <input type="hidden" id="cartTotalPrice" />
            <div class="fr total">合计：￥<span id="priceTotal">0.00</span></div>
            <div class="fr selected" id="selected">已选商品<span id="selectedTotal">0</span>件<span
                    class="arrow up">︽</span><span class="arrow down">︾</span></div>
            <div class="selected-view">
                <div id="selectedViewList" class="clearfix">
                    <div><img src="../images/1.jpg"><span>取消选择</span></div>
                </div>
                <span class="arrow">◆<span>◆</span></span>
            </div>
        </div>
    </div>



</body>
<script>
  
    var checkAll = document.getElementsByClassName("check-all")[0];  // 单元素
    var checkOneList = document.getElementsByClassName("check-one");  // 多元素
    var addList = document.getElementsByClassName("add");
    var reduceList = document.getElementsByClassName("reduce");
    var delList = document.getElementsByClassName("delete");
    var deleteAll = document.getElementById("deleteAll");
    var selectedTotal = document.getElementById("selectedTotal");
    var priceTotal = document.getElementById("priceTotal");

    // 事件委托:只是通过事件冒泡的原理将子代元素的事件委托给父元素,由父元素代为值 => 该获取的元素还是要获取

    var catbox = document.getElementsByClassName("catbox")[0];

    catbox.addEventListener("click",shoppingCarHandler);

    function shoppingCarHandler(e){
        // this -> 绑定事件的元素(catbox)
        // target -> 事件源(触发事件的元素)
        var e = e ||window.event;
        var target = e.target;
        // console.log(target);
        if(target.classList.contains("check-all")){
            console.log("点击了全选");
            // 所有的单选跟随全选的状态
            var status = target.checked;
            Array.from(checkOneList).forEach( checkOne => checkOne.checked = status)

        }else if(target.classList.contains("check-one")){
            console.log("点击了单选");
            isAllChecked();


        }else if(target.classList.contains("add")){
            console.log("点击了+");

             // a. 输入框  数量 加一 
             var countInput = target.previousElementSibling;
            console.log(countInput);

            var num = countInput.value; // num = "1"   取值
            num ++; // 自增
            countInput.value = num; // 赋值(更新到页面)

            // b. 小计更新  =  单价*数量
            // 数量 => num
            // 单价 => 
            
            var parentTd = target.parentElement;
            var priceTd = parentTd.previousElementSibling;
            var subtotalTd = parentTd.nextElementSibling;

            // "¥5999"  "5999元" => 如果有单位 需要进一步处理
            var price = priceTd.textContent;
            var subtotal = price * num;

            subtotalTd.textContent = subtotal.toFixed(2);


            // c. 减号按钮的 “-”  出现
            var reduce = countInput.previousElementSibling;
            reduce.textContent = "-";


        }else if(target.classList.contains("reduce")){
            console.log("点击了-");
        }else if(target.classList.contains("delete")){
            console.log("点击了单删");
        }else if(target.id === "deleteAll"){
            console.log("点击了全删");
        }
    }




    function isAllChecked() {
        // 假设法  => 先立一个假设(flag),然后尝试从对立面推翻它,  => 推翻了,假设不成立 没推翻=> 成立
        // var flag = true; // 假设全都被选中(假设)
        // // 然后尝试从对立面推翻它  => 有一个未选中的就可以推翻假设
        // for(var j=0;j<checkOneList.length;j++){
        //     var item = checkOneList[j];  // 每一个单选
        //     // item.checked == false
        //     // !item.checked == true => 才满足条件 => 取反前item.checked === false
        //     if(!item.checked){  // 存在未选中的 
        //         flag = false;  // 推翻假设
        //         break;
        //     }
        // }
        // checkAll.checked = flag;

        // every版  => 判断是否都选中
        var flag = Array.from(checkOneList).every(function (item) {
            return item.checked == true;
        })
        checkAll.checked = checkOneList.length > 0 ? flag : false;
    }


    // 结算=> 找到所有选中状态的checkOne => 对应找到商品的数量和小计 => 累加
    function getTotal() {
        var sum = 0; // 商品数量之和
        var allPrice = 0; // 商品的合计
        for (var j = 0; j < checkOneList.length; j++) {
            var checkOne = checkOneList[j];
            if (checkOne.checked) {  // 如果checkOne被选中  

                // 对应找到商品的数量和小计 
                var tr = parent(parent(checkOne));
                var countInput = tr.getElementsByClassName("count-input")[0];
                var subtotalTd = tr.getElementsByClassName("subtotal")[0];

                var num = countInput.value * 1;
                var subtotal = subtotalTd.textContent * 1;

                sum += num;
                allPrice += subtotal;
                
            }
        }
        selectedTotal.textContent = sum;
        priceTotal.textContent = allPrice.toFixed(2);
    }

    function parent(ele) {
        return ele.parentElement;
    }

    function prev(ele) {
        return ele.previousElementSibling;
    }

    function next(ele) {
        return ele.nextElementSibling;
    }



</script>

</html>